package com.slz.crm.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.slz.crm.common.enumeration.PermissionOperates;
import com.slz.crm.common.exiception.BaseException;
import com.slz.crm.common.untils.BaseUnit;
import com.slz.crm.pojo.entity.PermissionsEntity;
import com.slz.crm.pojo.vo.PermissionVO;
import com.slz.crm.server.mapper.PermissionsMapper;
import com.slz.crm.server.service.PermissionService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionsMapper, PermissionsEntity> implements PermissionService {
    @Resource
    private PermissionsMapper permissionsMapper;



    @Override
    public boolean hasPermission(Long userId, Enum<? extends PermissionOperates.Operation> targetPerm) {
        return hasPermission(targetPerm, getPermissionList(userId));
    }

    @Override
    public boolean hasPermission(Enum<? extends PermissionOperates.Operation> targetPerm, List<PermissionsEntity> permissionList) {
        if (permissionList.isEmpty()) {
            throw new BaseException("该用户没有权限");
        }
        List<Long> permissionIds = permissionList.stream().map(PermissionsEntity::getId).toList();

        Long permissionId;

        try {
            permissionId = (Long) targetPerm.getClass().getMethod("getId").invoke(targetPerm);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }


        return permissionIds.contains(permissionId);
    }

    @Override
    public List<PermissionsEntity> getPermissionList(Long userId) {
        return permissionsMapper.getPermissionList(userId);
    }


    @Override
    public List<PermissionVO> getMyPermission() {
        Long userId = BaseUnit.getCurrentId();

        List<PermissionsEntity> permissionList = getPermissionList(userId);

        List<PermissionVO> permissionVOList = new ArrayList<>();

        permissionList.forEach(permission -> {
            permissionVOList.add(new PermissionVO(permission));
        });

        return permissionVOList;
    }

    @Override
    public List<String> getKeyList() {

        List<String> keyList = new ArrayList<>();

        keyList.add("客户管理模块权限");
        keyList.add("销售管理模块权限");
        keyList.add("财务管理模块权限");
        keyList.add("联络任务模块权限");
        keyList.add("统计报表模块权限");
        keyList.add("权限管理模块权限");
        keyList.add("隐私信息查看权限");

        return keyList;
    }

    @Override
    public List<PermissionVO> getPermissionList(PermissionsEntity permissionsEntity) {
        List<PermissionsEntity> permissionList = new ArrayList<>();

        LambdaQueryWrapper<PermissionsEntity> queryWrapper = new LambdaQueryWrapper<>();
        if(permissionsEntity.getId()!=null){
            queryWrapper.like(PermissionsEntity::getId, permissionsEntity.getId());
        }
        if(permissionsEntity.getPermissionsName()!=null){
            queryWrapper.like(PermissionsEntity::getPermissionsName, permissionsEntity.getPermissionsName());
        }
        if(permissionsEntity.getPermissionsDesc()!=null){
            queryWrapper.like(PermissionsEntity::getPermissionsDesc, permissionsEntity.getPermissionsDesc());
        }
        permissionList = permissionsMapper.selectList(queryWrapper);

        return permissionList.stream().map(PermissionVO::new).toList();
    }
}
